package com.yuntsg.ruiijn.databse_search.service;

import com.yuntsg.ruiijn.databse_search.entity.FileDetails;
import com.yuntsg.ruiijn.databse_search.entity.FileDetailsTxt;
import common.util.ExtractUtil;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 葛成瑞
 * @Date: 2024/6/19 12:14
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ExtractFilesTextAsync {

    private final ExtractUtil extractUtil;

    private final FileDetailService detailService;

    private final FileDetailTxtService detailTxtService;


    /**
     * @param inputStream
     * @param rid
     * @param userId
     * @param type        1 word ,2 pdf
     *                                                                                 todo 纯文本模式    与上面一样 只是入库不一致
     */
    @SneakyThrows
    @Async
    public void extractFilesText(InputStream inputStream, Integer rid, Integer userId, Integer type, String path, Integer spilt) {
        List<FileDetails> fileDetails = new ArrayList<>();
        if (type == 0) {
            fileDetails = extractUtil.extract0(rid, userId, path);
        } else if (type == 1) {
            fileDetails = extractUtil.extractWord(inputStream, rid, userId);
        } else if (type == -1) {
            fileDetails = extractUtil.extractPdfBox(rid, userId, path);
        } else if (type == -2) {
            fileDetails = extractUtil.extractPdfBoxForFull(rid, userId, path);
        } else {
            fileDetails = extractUtil.extractPdf(inputStream, rid, userId);
        }
        if (fileDetails.size() == 0) {
//            return false;
        }
        List<FileDetailsTxt> insList = new ArrayList<>(100000);
        for (FileDetails fileDetail : fileDetails) {
            FileDetailsTxt newSin = new FileDetailsTxt();
            BeanUtils.copyProperties(fileDetail, newSin);
            insList.add(newSin);
        }
        insList.stream().forEach(f -> {
            detailTxtService.saveOrUpdate(f.setFId(rid));
            if (type == -2) {
                return;
            }

        });
//        return true;
    }


}
